package com.ys.web.user.manager;

import com.ys.utils.base.j2se.StringHandler;
import com.ys.web.user.info.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 注销用户缓存
 */
@Slf4j
@Service
public class UserLogoutManager implements Runnable{


    private static List<Map<String,Object>> AlList = new ArrayList<>();
    public static Map<Long,Integer> map = new HashMap();

    @Autowired
    private UserInfoService userInfoService;
    @PostConstruct
    public void init () {
        new Thread(this).start();
    }
    @Override
    public void run() {
        while (true) {
            try {
                this.load();
                Thread.sleep(5*60*1000);
            } catch (Exception e) {
                log.error("UserLogoutManager",  e);
            }
        }
    }

    private void load() {
        log.info("UserLogoutManager---------------begin");
        try {
            AlList = userInfoService.userLogout();
            if(StringHandler.isNotEmpty(AlList)){
                map = new HashMap<>();
                for (Map<String,Object> obj:AlList) {
                    map.put(StringHandler.getLong(obj.get("id")),StringHandler.getInt(obj.get("account_status")));
                }
            }else{
                map = new HashMap<>();
            }
        }catch (Exception e){
            log.error("UserLogoutManager------------加载错误",e);
        }
        log.info("UserLogoutManager---------------end");
    }

    public static int account_status(long id){
        if(StringHandler.isEmpty(map)){
            return 0;
        }
        if(map.size()>0){
            if(map.containsKey(id)){
                return map.get(id);
            }
            return 0;
        }
        return 0;
    }
}
